package trunk.backend;

import java.util.ArrayList;
import java.io.*;

public class Page {
	private ArrayList<HTMLElement> page;
	
	public Page() {
		page = new ArrayList<HTMLElement>();
	}
	
	public void addLabel(String labelText, boolean newLine) {
		if (labelText == null) return;
		if (newLine) labelText += "\n<br />\n";
		page.add(new HTMLLabel(labelText));
	}
	
	public void addTextBox(String label, String name) {
		if (label == null) label = "";
		if (name == null) return;
		page.add(new HTMLTextBox(label, name));
	}
	
	public void addTextBox(String name) {
		if (name == null) return;
		page.add(new HTMLTextBox(name));
	}
	
	public void addRadioButton(String name, ArrayList<String> labels, ArrayList<String> values, boolean vertical) {
		if (name == null) return;
		if (labels == null || values == null || labels.size() != values.size()) return;
		page.add(new HTMLRadioButton(name, labels, values, vertical));
	}
	
	public void addCheckBox(String name, String label, String value) {
		if (name == null || label == null || value == null) return;
		page.add(new HTMLCheckBox(name, label, value));
	}
	
	public void addCheckBoxes(String name, ArrayList<String> labels, ArrayList<String> values, boolean vertical) {
		if (name == null) return;
		if (labels == null || values == null || labels.size() != values.size()) return;
		page.add(new HTMLCheckBox(name, labels, values, vertical));
	}
	
	public void addLineBreak() {
		page.add(new HTMLLabel("<br />"));
	}
	
	public void addParagraph() {
		page.add(new HTMLLabel("<p />"));
	}
	
	public void startParagraph() {
		page.add(new HTMLLabel("<p>"));
	}
	
	public void endParagraph() {
		page.add(new HTMLLabel("</p>"));
	}
	
	public String toString() {
		String end, html;
	    html = "<html>\n<body>\n";
	    html += "<form>\n\n";
	    end = "</form>\n</body>\n</html>";
	    for (HTMLElement h : page) {
	    	html += h.toHTML();
	    }
	    html += "\n";
	    html += end;
	    return html;
	}
	
	public String toString(String email) {
		String end, buttons, html;
	    html = "<html>\n<body>\n";
	    html += "<form action=\"MAILTO:" + email + "\" " +
	    		"method=\"post\" enctype=\"text/plain\">\n\n";
	    buttons = "<input type=\"submit\" value=\"Send\">\n" +
	    			"<input type=\"reset\" value=\"Reset\">\n";
	    end = "</form>\n</body>\n</html>";
	    for (HTMLElement h : page) {
	    	html += h.toHTML();
	    }
	    html += "<p />\n";
	    html += buttons;
	    html += "\n";
	    html += end;
	    return html;
	}
	
	public void writeHTML(String writeTo) {
		FileOutputStream fos; 
	    DataOutputStream dos;

	    try {
	      File file = new File(writeTo);
	      fos = new FileOutputStream(file);
	      dos = new DataOutputStream(fos);
	      dos.writeChars(this.toString());
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	}
	
	public void writeHTML(String writeTo, String email) {
		FileOutputStream fos; 
	    DataOutputStream dos;

	    try {
	      File file = new File(writeTo);
	      fos = new FileOutputStream(file);
	      dos = new DataOutputStream(fos);
	      dos.writeChars(this.toString(email));
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	}
}
